package me.jessyan.mvparms.demo.mvp.ui.activity.coupon;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DeviceUtils;
import com.jess.arms.utils.PerfectClickListener;
import com.jess.arms.utils.StautBarUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import me.jessyan.mvparms.demo.R;

import me.jessyan.mvparms.demo.di.component.coupon.DaggerGetCouponComponent;
import me.jessyan.mvparms.demo.mvp.contract.coupon.GetCouponContract;
import me.jessyan.mvparms.demo.mvp.presenter.coupon.GetCouponPresenter;
import me.jessyan.mvparms.demo.mvp.ui.activity.store.MainStoreActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.store.StoreActivity;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * 领券中心
 */
public class GetCouponActivity extends BaseActivity<GetCouponPresenter> implements GetCouponContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.toolbar)
    CustomToolbar toolbar;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerGetCouponComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_get_coupon; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        StautBarUtils.setStatusMode(this, false, true);
        StautBarUtils.setBarPadding(findViewById(R.id.cl));

        initRecyclerView();
        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {

            }
        });

    }

    private void initRecyclerView() {
        List<String> list = new ArrayList<>();
        list.add("s");
        list.add("s");
        list.add("s");

        LinearLayoutManager lr = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(lr);
        ArmsUtils.configRecyclerView(recyclerView,lr);

        DefaultAdapter adapter =new DefaultAdapter(list,false) {
            @Override
            public BaseHolder getHolder(View v, int viewType) {
                return new BaseHolder(v) {
                    @Override
                    public void setData(Object data, int position) {
                      ImageView iconIv = v.findViewById(R.id.icon_iv);
                        TextView toUserTv = v.findViewById(R.id.to_user_tv);
                        iconIv.setOnClickListener(new PerfectClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {
                                if(mOnItemClickListener!=null){
                                    mOnItemClickListener.onItemClick(v,viewType,data,position);
                                }
                            }
                        });
                        toUserTv.setOnClickListener(new PerfectClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {
                                if(mOnItemClickListener!=null){
                                    mOnItemClickListener.onItemClick(v,viewType,data,position);
                                }
                            }
                        });
                    }
                };
            }

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_coupon_fuck;
            }


        };

        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int viewType, Object data, int position) {
                if(view instanceof ImageView){//跳转店铺
                    Intent intent = new Intent(GetCouponActivity.this, StoreActivity.class);
//                    intent.putExtra("bean", positionData);
                    ActivityOptionsCompat options =
                            ActivityOptionsCompat.makeSceneTransitionAnimation(GetCouponActivity.this,
                                    view, getResources().getString(R.string.transition_store_img));//与xml文件对应
                    ActivityCompat.startActivity(GetCouponActivity.this, intent, options.toBundle());
                }else if (view instanceof  TextView){// 跳转抢券
                    Intent intent = new Intent(GetCouponActivity.this,CouponDetailActivity.class);
                    intent.putExtra("type", CouponDetailActivity.COUPON_ROB);
                    intent.putExtra("isPastDue",false);
                    startActivity(intent);
                }else {

                }


            }
        });



    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


}
